<?php

namespace app\models;

use think\Db;
use think\Model;

class ShopLog extends Model
{
    protected $pk = 'id';
    protected $autoWriteTimestamp = 'datetime';
    protected $createTime = 'created';

    /**
     * 用户金额变更记录
     * @param $shop   // 商户信息
     * @param $money   // 商户信息
     * @param $type   // 类型
     * @param $info   // 备注
     * @param $source   // 来源ID
     * @return void
     */
    public static function MoneyEdit($shop, $money = 0, $type = 0, $info = '', $source = 0)
    {
        if ($money != 0) {
            $res = (new Shoplist())->where(['id' => $shop['id'], 'money' => $shop['money']])->setInc('money', $money);
            $res2 = (new ShopLog())->insert([
                'timeid' => time(),
                'shopid' => $shop['id'],
                'befores' => $shop['money'],
                'money' => $money,
                'after' => ($shop['money'] + $money),
                'type' => $type,
                'info' => $info,
                'dates' => date('Y-m-d'),
                'source' => $source
            ]);
            if (!$res || !$res2) {
                throw new \Exception("资产修改失败，请重试", 100);
            }
        }
    }
}
